% Compute the global solution using a paramerized expectations algorithm
% Dynamic bank run model
% Bank leverage choice only
% Capital depreciation shock

close all
warning off

load('policy_tau_5_always')

%% Generate random shocks

options = optimset;
T = 10000;               % simulation periods
t_drop = 1000;

rng(1)                   % same random shock 
shocks = normrnd(0,1,T+1,1);  % random shock
%Exi = zeros(T+1);  % stochastic steady state

kv   = zeros(T+1,1);
rbv  = zeros(T+1,1);
nv   = zeros(T+1,1);
xiv  = zeros(T+1,1);
hv   = zeros(T+1,1);
cv   = zeros(T+1,1);
yv   = zeros(T+1,1);
iv   = zeros(T+1,1);
lv   = zeros(T+1,1);
pibv = zeros(T+1,1);
runv = zeros(T+1,1);
rhseev = zeros(T+1,1);
rkhat  = zeros(T+1,1);
rkhats = zeros(T+1,1);
prv  = zeros(T+1,1);
av   = zeros(T+1,1);
pol_flag   = zeros(T+1,1);

rhsee_derived_temp = zeros(T+1,1);
rbar_derived       = zeros(T+1,1);
rbar_gap           = zeros(T+1,1);

erk_next       = zeros(T+1,1);
erk_next_unreg = zeros(T+1,1);
kv_unreg = zeros(T+1,1);
lv_unreg = zeros(T+1,1);

%% Simulation

shocks = zeros(T+1,1);

pol1_t = 0;
pol2_t = 0;

kv(1)  = k_ss;
rbv(1) = rb_ss;
nv(1)  = n_ss;
lv(1)  = kv(1)/nv(1);
av(1)  = a_ss;

    for t=2:T+1
        av(t) = rho_a*av(t-1) + sigma_a*shocks(t-1);
        hv(t)  = ((1-alpha)/psi*exp(av(t))*kv(t-1)^(alpha))^(1/(alpha+1/nu));
        yv(t)  = exp(av(t)) * kv(t-1)^(alpha)*hv(t)^(1-alpha);

        rkhats(t) = log ( rbv(t-1) * (1-1/lv(t-1)) * ( 1+lambda*(1-gamma) ) - (1-delta) );
        rkhat(t)  = log ( alpha*((1-alpha)/psi)^((1-alpha)/(alpha+1/nu)) ) ...
                  + (1+nu)/(alpha*nu+1)*av(t) - (1-alpha)/(alpha*nu+1)*log(kv(t-1));
        xiv(t)  = exp(rkhat(t))/exp(rkhats(t));

        K  = [kv(t-1) rbv(t-1) nv(t-1) xiv(t)];
        x0 = (log(K)).^expmx;
        x  = prod(x0,2)';
        if xiv(t) >= 1
            pibv(t)  = ( exp(rkhat(t)) + 1-delta )*lv(t-1)*nv(t-1) - rbv(t-1)*(lv(t-1)-1)*nv(t-1) - nv(t-1);

            if pibv(t) > 0
                rhseev(t) = exp(x*solved_eta1_rhsee);
                rbv(t)    = exp(x*solved_eta1_rbar);
                nv(t) = chi1*( chi0*pibv(t) + nv(t-1) ) + n0;
            else
                rhseev(t) = exp(x*solved_eta3_rhsee);
                rbv(t)    = exp(x*solved_eta3_rbar);
                nv(t) = chi1*(      pibv(t) + nv(t-1) ) + n0;
            end

        else
            pibv(t)  = ( exp(rkhat(t)) + 1-delta )*lv(t-1)*nv(t-1) - rbv(t-1)*(1+lambda*(1-gamma))*(lv(t-1)-1)*nv(t-1) - nv(t-1);
            nv(t) = n_ss*(1 - nd/100);
            rhseev(t) = exp(x*solved_eta2_rhsee);
            rbv(t)    = exp(x*solved_eta2_rbar);
            runv(t) = 1;
        end

        cv(t) = rhseev(t)^(-1) + psi*hv(t)^(1+1/nu)/(1+1/nu);
        iv(t) = yv(t) - cv(t);
        kv(t) = (1-delta)*kv(t-1) + iv(t);

        lv(t)  = kv(t)/nv(t);

        erk_next(t) = log ( alpha*((1-alpha)/psi)^((1-alpha)/(alpha+1/nu)) ) ...
                    + (1+nu)/(alpha*nu+1)*rho_a*av(t) - (1-alpha)/(alpha*nu+1)*log(kv(t));

        % check policy
        % check policy
        if pibv(t)>=thre_pib && nv(t)>thre_n
            
            lv_unreg(t) = lv(t)/(1-tau);
            kv_unreg(t) = lv_unreg(t)*nv(t);
            erk_next_unreg(t) = log ( alpha*((1-alpha)/psi)^((1-alpha)/(alpha+1/nu)) ) ...
                              + (1+nu)/(alpha*nu+1)*rho_a*av(t) - (1-alpha)/(alpha*nu+1)*log(kv_unreg(t));
            rbv(t) = fzero('func_Rb',rbv(t),options,lv_unreg(t),erk_next_unreg(t),lambda,gamma,delta,sigma_rk);

        else
            lv_unreg(t) = lv(t);
            kv_unreg(t) = kv(t);
            erk_next_unreg(t) = erk_next(t);
            rbv(t) = fzero('func_Rb',rbv(t),options,lv(t),erk_next(t),lambda,gamma,delta,sigma_rk);
        end

        rkhats_next = log ( rbv(t) * (1-1/lv(t)) * ( 1+lambda*(1-gamma) ) - (1-delta) );
        z_temp     = (rkhats_next - erk_next(t)) / sigma_rk;
        cdf = normcdf(z_temp);
        prv(t) = cdf;

    end
    % End of T period simulation
    
stss_n_pol  = nv(5000);
stss_k_pol  = kv(5000);
stss_rb_pol = rbv(5000);
stss_y_pol  = yv(5000);
stss_c_pol  = cv(5000);
stss_i_pol  = iv(5000);
stss_h_pol  = hv(5000);
stss_l_pol  = lv(5000);
stss_pib_pol  = pibv(5000);
stss_pr_pol   = prv(5000);
stss_rkhat_pol   = rkhat(5000);
stss_rkhats_pol  = rkhats(5000);
